package mark.tom.friend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import com.alibaba.android.arouter.launcher.ARouter;
import com.wardellbagby.rxcountdowntimer.RxCountDownTimer;
import io.reactivex.functions.Consumer;
import mark.tom.friend.app.CustomApplication;
import mark.tom.friend.base.activity.BaseActivity;
import mark.tom.friend.component.DaggerIndexComponent;
import mark.tom.friend.consts.ActivityRouterConsts;
import mark.tom.friend.contract.IndexContract;
import mark.tom.friend.module.IndexModule;
import mark.tom.friend.presenter.IndexPresenter;
import mark.tom.friend.roll.R;

import javax.inject.Inject;
import java.util.concurrent.TimeUnit;

public class IndexActivity extends BaseActivity implements IndexContract.View {

    @Inject IndexPresenter mPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.layout_index;
    }

    @Override
    protected void onCreate(View contentView, Bundle savedInstanceState) {
        // 设置为退出确认模式
        setBackAwayMode(BACK_WAY_DOUBLE);

        DaggerIndexComponent.builder()
                .appComponent(CustomApplication.get(this).getAppComponent())
                .indexModule(new IndexModule(this))
                .build()
                .inject(this);

        // 使用路由框架初始化
        ARouter.getInstance().inject(this, contentView, savedInstanceState);
        // 绑定视图事件
        bindViewListener();
    }

    @SuppressLint("CheckResult")
    private void bindViewListener() {
        // 开启倒计时
        RxCountDownTimer.create(3, 1, TimeUnit.SECONDS)
                .compose(this.<Long>bindToLifecycle())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long remainTime) throws Exception {
                        if (remainTime == 0) {
                            // 跳转到主界面
                            directToMainPage();
                        }
                    }
                });
    }

    private void directToMainPage() {
        // 跳转到选择APP界面
        ARouter.getInstance()
                .build(ActivityRouterConsts.ROUTER_APP_MAIN)
                .direction(IndexActivity.this);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 回收mPresenter
        mPresenter.onDestroy();
    }
}
